package com.graduation.rescuematerial.controller.system;

import com.graduation.rescuematerial.service.CompanyService;
import com.graduation.rescuematerial.service.IUserCompanyService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/24 23:05
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/sys/register")
@Api(tags = "管理员操作——添加部门，用户")
public class SystemRegisterController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;
    @Autowired
    private IUserCompanyService userCompanyService;

    @ApiOperation("添加用户（同时分配权限）")
    @ResponseBody
    @PostMapping("/user")
    @Transactional(rollbackFor = Exception.class)
    public RespBean registerUser(@RequestBody RegisterUserVo registerUserVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=registerUserVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                //添加用户
               if(userVo.getAction().equals("system_administrator")){
                   return userService.registerUser(registerUserVo);
               }else if (userVo.getAction().equals("organization_administrator")){
                   registerUserVo.setCompanyName(userVo.getCompanyName());
                   return userService.registerUser(registerUserVo);
               }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("添加部门")
    @ResponseBody
    @PostMapping("/company")
    public RespBean registerDepartment(@RequestBody RegisterCompanyVo registerCompanyVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=registerCompanyVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")){
                    return companyService.registerDepartment(registerCompanyVo);
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

//    @ApiOperation("给部门添加用户或给用户添加部门（只能添加没有加入部门的用户）")
//    @ResponseBody
//    @PostMapping("/user/company")
//    public RespBean registerUserCompany(@RequestBody RegisterUserCompanyVo registerUserCompanyVo,HttpServletRequest request, HttpServletResponse response){
//        String ticket=registerUserCompanyVo.getTicket();
//        if(ticket!=null && !ticket.isEmpty()){
//            //根据cookie获取用户信息
//            UserVo userVo=userService.getUserByCookie(ticket,request,response);
//            if(userVo!=null ){
//                if(userVo.getAction().equals("system_administrator")||userVo.getAction().equals("organization_administrator")){
//                    return userCompanyService.addOne(registerUserCompanyVo);
//                }
//                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION);
//            }
//        }
//        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
//    }


}
